package view.component.main
{
	
	import embeds.EmbedRes;
	
	import enums.main.UISize;
	
	import feathers.controls.Button;
	import feathers.controls.Label;
	import feathers.controls.Screen;
	
	import starling.animation.Tween;
	import starling.core.Starling;
	import starling.display.Image;
	import starling.events.Event;
	import starling.textures.Texture;
	
	import util.FG2FUGenerator;
	
	import view.component.IAnimateNaviView;
	import view.event.main.LoadViewEvent;
	
	
	/**
	 * 加载数据界面
	 * @author £iang
	 * 
	 */	
	public class LoadView extends Screen implements IAnimateNaviView
	{
		//-----------------------------
		// variables
		//-----------------------------
		
		/** 加载信息显示文本 */
		public var loadInfoLabel:Label;
		/** 中间按钮 */
		public var loadButton:Button;
		/** Loading图标数组 */
		public var loadIconVec:Vector.<Image>;
		
		//-----------------------------
		// properties
		//-----------------------------
		
		//-----------------------------
		// setter&getter
		//-----------------------------
		
		//-----------------------------
		// methods
		//-----------------------------
		
		/**
		 * 初始化函数 
		 * 
		 */		
		override protected function initialize():void
		{
			loadInfoLabel = new Label();
			loadInfoLabel.name = "smallCenterTitleLabel";
			this.addChild( loadInfoLabel );
			
			loadButton = FG2FUGenerator.generateRoundRectButton(
				220, 88, dpiScale, 16, FG2FUGenerator.TEXT_SIZE_LARGE );
			loadButton.label = "Loading";
			this.addChild( loadButton );
			
			var tmpImage:Image;
			var tmpTexture:Texture;
			
			loadIconVec = new Vector.<Image>;
			
			for ( var i:int = 0; i < 5; i ++ )
			{
				tmpTexture = Texture.fromBitmapData( new EmbedRes[ "LOADING_ICON" + ( i + 1 ) ]().bitmapData );
				tmpImage = new Image( tmpTexture ); 
				tmpImage.scaleX = tmpImage.scaleY = this.dpiScale;
				tmpImage.alpha = 0;
				loadIconVec.push( tmpImage );
				this.addChild( loadIconVec[ i ] );
			}
			
			addListener();
		}
		
		
		/**
		 * 绘制函数 
		 * 
		 */		
		override protected function draw():void
		{
			loadInfoLabel.y = ( UISize.MAIN_H - loadInfoLabel.height ) / 2 
				- 125 * this.dpiScale;
			loadInfoLabel.width = UISize.MAIN_W;
			loadInfoLabel.pivotX = UISize.MAIN_W / 2;
			loadInfoLabel.x = UISize.MAIN_W / 2;
			
			var buttonW:Number = loadButton.width;
			
			loadButton.pivotX = buttonW / 2;
			loadButton.x = UISize.MAIN_W / 2;
			loadButton.y = ( UISize.MAIN_H - loadButton.height ) / 2 
				+ 125 * this.dpiScale;
			loadButton.touchable = false;
			
			var curIcon:Image;
			var indent:Number = 100;
			
			for ( var i:int = 0; i < 5; i ++ )
			{
				curIcon = loadIconVec[ i ] as Image;
				curIcon.x = ( UISize.MAIN_W - curIcon.width ) / 2 + indent * ( i - 2 ) * this.dpiScale;
				curIcon.y = ( UISize.MAIN_H - curIcon.height ) / 2 + 50 * this.dpiScale;
			}
		}
	
		/**
		 * 添加侦听器 
		 * 
		 */		
		public function addListener():void
		{
			loadButton.addEventListener( Event.TRIGGERED, loadButtonTriggeredHandler );
		}
		
		/**
		 * 更新显示加载信息 
		 * @param info 加载信息
		 * 
		 */		
		public function setInfoLabelText( info:String ):void
		{
			loadInfoLabel.text = info;
		}
			
		/**
		 * 播放加载阶段完成的动画 
		 * @param index 阶段编号
		 * 
		 */		
		public function playLoadIconAnimation( index:int ):void
		{
			var targetIcon:Image = loadIconVec[ index ] as Image;
			var tween:Tween = new Tween( targetIcon, 1, "easeOut" );
			
			tween.animate( "y", ( UISize.MAIN_H - targetIcon.height ) / 2 );
			tween.animate( "alpha", 1 );
			Starling.juggler.add( tween );
		}
		
		/**
		 * 可以开始浏览wiki 
		 * 
		 */		
		public function startWiki():void
		{
			this.loadInfoLabel.text = "MH4 Wiki"
			this.loadButton.alpha = 0;
			this.loadInfoLabel.alpha = 0;
			
			this.loadButton.label = "Open";
			
			var tween:Tween = new Tween( loadButton, 0.7, "linear" );
			
			tween.animate( "alpha", 1 );
			tween.delay = 0.5;
			Starling.juggler.add( tween );
			
			var tween2:Tween  = new Tween( loadInfoLabel, 0.7, "linear" );
		
			tween2.animate( "alpha", 1 );
			tween2.delay = 0.5;
			tween2.onComplete = function ():void { loadButton.touchable = true; };
			Starling.juggler.add( tween2 );
		}
		
		//-----------------------------
		// events
		//-----------------------------
		
		/**
		 *  处理点击open按钮后派发的事件 
		 * @param event 点击open按钮后派发的事件 
		 * 
		 */		
		protected function loadButtonTriggeredHandler( event:Event ):void
		{
			this.touchable = false;
			dispatchEventWith( "open" );
		}
		
		/**
		 * 播放显示动画
		 * 
		 */	
		public function playShowAnimate():void
		{
			dispatchCommonEvent( new LoadViewEvent( LoadViewEvent.START_LOAD ) );
		}
		
		/**
		 * 播放隐藏动画 
		 * 
		 */	
		public function playHideAnimate():void
		{
			var tmpIcon:Image;
			var tmpTween:Tween;
			
			tmpTween = new Tween( loadInfoLabel, 0.15 );
			tmpTween.animate( "scaleX", 0 );
			Starling.juggler.add( tmpTween );
			
			tmpTween = new Tween( loadButton, 0.15 );
			tmpTween.animate( "scaleX", 0 );
			Starling.juggler.add( tmpTween );
			
			for ( var i:int = 0; i < 5; i ++ )
			{
				tmpIcon = loadIconVec[ i ] as Image;
				tmpTween = new Tween( tmpIcon, 0.15 );
				tmpTween.delay = i * 0.15;
				tmpTween.animate( "alpha", 0 );
				tmpTween.animate( "y",  ( UISize.MAIN_H - tmpIcon.height ) / 2 + 50 * this.dpiScale );
				Starling.juggler.add( tmpTween );
			}
		}
		
		//-----------------------------
		// dispose
		//-----------------------------
		
		/**
		 * 析构函数 
		 * 
		 */		
		override public function dispose():void
		{
			loadButton.removeEventListeners();
			Starling.juggler.removeTweens( loadButton );
			Starling.juggler.removeTweens( loadInfoLabel);
			
			var len:int = loadIconVec.length;
			
			loadInfoLabel.dispose();
			loadInfoLabel = null;
			
			loadButton.dispose();
			FG2FUGenerator.deepDisposeButtonComp( loadButton );
			loadButton = null;
			
			for ( var i:int = 0; i < len; i ++ )
			{
				Starling.juggler.removeTweens( loadIconVec[ i ] );
				( loadIconVec[ i ] as Image ).dispose();
				( loadIconVec[ i ] as Image ).texture.dispose();
				loadIconVec[ i ] = null;
			}
			
			loadIconVec.length = 0;
			loadIconVec = null;
			super.dispose();
		}
		
		//-----------------------------
		// construction
		//-----------------------------
		
		/**
		 * 构造函数
		 *
		 */
		public function LoadView()
		{
			super();
		}
	}
}